package com.lyf.core.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lyf.core.compant.Perm;
import com.lyf.core.log.aspect.OptType;
import com.lyf.core.pojo.Department;
import com.lyf.core.pojo.Menu;
import com.lyf.core.pojo.Role;
import com.lyf.core.pojo.User;
import com.lyf.core.pojo.model.PageModel;
import com.lyf.core.pojo.model.ResultModel;
import com.lyf.core.service.DeptService;
import com.lyf.core.service.MenuService;
import com.lyf.core.service.RoleService;
import com.lyf.core.service.UserService;

@Controller
@RequestMapping("/ma")
@Perm(id="ma" , model="综合管理" , name="综合管理" ,isPage=false )
public class Manager {

	@Autowired
	private UserService userService ;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private MenuService menuService;
	
	@Autowired
	private DeptService deptService;
	
	@RequestMapping("/users") 
	@Perm(id="ma_users" , model="基础数据",parentId="base_ma" , name="用户页面" )
	public String users() {
		return null ;
	}
	
	@RequestMapping("/roles") 
	@Perm(id="ma_roles" , model="基础数据" ,parentId="base_ma", name="用户角色页面" )
	public String roles() {
		return null;
	}
	
	@RequestMapping("/menus") 
	@Perm(id="ma_menus" , model="基础数据" ,parentId="base_ma", name="菜单页面" )
	public String menus() {
		return null;
	}
	
	@RequestMapping("/depts") 
	@Perm(id="ma_depts" , model="基础数据" ,parentId="base_ma", name="机构信息" )
	public String depts() {
		return null;
	}
	
	// #################################### 组织机构 开始 #######################################
	@RequestMapping("/findDepts") 
	@ResponseBody
	@Perm(id="ma_findDepts" , model="基础数据" ,parentId="ma_depts", name="机构查询"  ,isPage=false)
	public List<Department> findDepts(Integer id  , Boolean iterChilds){
		if(id == null)
			id = 0;
		if(iterChilds == null)
			iterChilds = false;
		return deptService.findByParent(id , iterChilds);
		
	}
	
	/**
	 * 
	 * @param dept
	 * @return
	 */
	@RequestMapping("/removeDept")
	@ResponseBody
	
	@Perm(id="ma_removeDept" , model="机构管理" ,parentId="ma_depts", name="机构移除" ,isPage=false , optType=OptType.REMOVE )
	public ResultModel removeDept(Integer did) {
		ResultModel model = new ResultModel();
		
		if(did == null){ 
			model.setError( "没有选中部门");
			return model ;
		}
		 
		String error = deptService.delete(did);
		if(error != null){
			model.setError(error);
			return model ;
		}
		  
		model.setSuccess();
		   
		return model ;
		
	}
	 
	 
	/**
	 * 
	 * @param dept
	 * @return
	 */
	@RequestMapping("/addDept")
	@ResponseBody
	
	@Perm(id="ma_addDept" , model="机构管理" ,parentId="ma_depts", name="机构添加" ,isPage=false, optType=OptType.SAVE)
	public ResultModel addDept(Department dept) {
		ResultModel model = new ResultModel();
		
		if(dept == null){ 
			model.setError( "部门信息为空");
			return model ;
		}
		
		User u = getSessionUser();
		
		dept.setCreate_uid(u.getUser_id());
		
		String error = deptService.add(dept);
		if(error != null) {
			model.setError(error);
		} else {
			  
			model.setSuccess();
			model.put("data", dept);
		}
		
		
		return model ;
		
	}
	
	/**
	 * 
	 * @param dept
	 * @return
	 */
	@RequestMapping("/updateDept")
	@ResponseBody

	@Perm(id="ma_updateDept" , model="机构管理" ,parentId="ma_depts", name="机构更新"  ,isPage=false , optType=OptType.UPDATE)
	public ResultModel updateDept(Department dept) {
		ResultModel model = new ResultModel();
		
		if(dept == null || dept.getDid() == null){ 
			model.setError( "部门信息为空");
			return model ;
		}
		 
		
		deptService.update(dept);
		 
		model.setSuccess();
		model.put("data", dept);
		  
		return model ;
		
	}
	
	
	
	// #################################### 组织机构 结束 #######################################

	
	// ################################ 用户  开始 ###########################################
	
	/**
	 * 查找用户信息
	 * @param key 查找用户的关键字,包含有用户名、ID
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/findUsers")
	@ResponseBody  
	@Perm(id="ma_findUser" , model="基础数据" , name="获取用户列表" ,parentId="ma_users",isPage=false)
	public PageModel<User> findUsers(String key  , Integer dept_id, Integer page, Integer rows ) {
		  
		if(page == null) page = 1;
		if(rows == null) rows = 20;
		  
		PageModel<User> model = userService.findUsers(dept_id , key, rows, page);
   
		 
		return model ;
		 
	} 
	
	@RequestMapping("/removeUser")
	@ResponseBody  
	@Perm(id="ma_removeUser" , model="基础数据" , name="删除用户" ,parentId="ma_users",isPage=false , optType=OptType.REMOVE)
	public ResultModel removeUser(Integer uid){
		ResultModel model = new ResultModel();
		
		if(uid == null || uid <= 0){
			model.setError("待删除用户为空");
			return model ;
		}
		
		userService.deleteUser(uid);
		
		model.setSuccess();
		
		return model;
	}
	
	@RequestMapping("/resetPass")
	@ResponseBody  
	@Perm(id="ma_resetPass" , model="基础数据" , name="密码重置" ,parentId="ma_users",isPage=false , optType=OptType.UPDATE)
	public ResultModel resetPass(Integer uid , String pass){
		ResultModel model = new ResultModel();
		
		if(uid == null || uid <= 0){
			model.setError("待重置密码用户为空");
			return model ;
		}
		if(pass == null || pass.trim().equals("")) {
			model.setError("重置密码为空");
			return model ;
		}
		
		userService.restPass(uid, pass);
		
		model.setSuccess();
		
		return model;
	}
	
	@RequestMapping("/updateUser")
	@ResponseBody  
	@Perm(id="ma_updateUser" , model="基础数据" , name="用户信息更新" ,parentId="ma_users",isPage=false , optType=OptType.UPDATE)
	public ResultModel updateUser(User user){
		ResultModel model = new ResultModel();
		
		if(user == null || user.getUser_id() == null || user.getId() == null || user.getId() <= 0){
			model.setError("更新用户为空");
			return model ;
		}
		
		String error = userService.update(user);
		if(error != null){
			model.setError(error);
			return model ;
		}
		
		//绑上部门名称 和角色名称
		user = userService.findById(user.getId());
			
		
		model.put("data", user);
		model.setSuccess();
		
		return model;
	}
	

	@ResponseBody
	@RequestMapping("/addUser") 
	@Perm(id="ma_addUser" , model="基础数据" , name="添加用户" ,parentId="ma_users",isPage=false , optType=OptType.SAVE)
	public ResultModel addUser(User u) {
		
		ResultModel model = new ResultModel();
		 
		String error = userService.save(u);
		if(error != null){
			model.setError(error);
			return model ;
		}
		
		//回填部门信息
		u = userService.findById(u.getId());
		model.put("data", u);
		model.setSuccess();
		return model ;
		
	}
	
	//########################### 用户 结束 ##########################################
	
	//########################### 角色 ############################################
	
	/**
	 * 查找角色信息 
	 * @return
	 */
	@RequestMapping("/findRoles")
	@ResponseBody 
	@Perm(id="ma_findRole" , model="基础数据" , name="获取角色列表" ,parentId="ma_roles",isPage=false )
	public List<Role> findRoles() {
		return roleService.findAll();
		 
	}
	
	@ResponseBody
	@RequestMapping("/addRole")  
	@Perm(id="ma_addRole" , model="基础数据" , name="添加角色" ,parentId="ma_roles",isPage=false , optType=OptType.SAVE)
	public ResultModel addRole(Role role  ) {
		
		ResultModel model = new ResultModel();
		 
		String res = roleService.save(role);
		if(res != null){
			model.setError(res);
		}else{
			model.put("data", role);
			model.setSuccess();
		}
		 
		return model ;
		 
		
	}
	@ResponseBody
	@RequestMapping("/removeRole")  
	@Perm(id="ma_removeRole" , model="基础数据" , name="删除角色" ,parentId="ma_roles",isPage=false , optType=OptType.REMOVE)
	public Map<String, Object> removeRole(int role_id ) {
		
		Map<String, Object> model = new HashMap<String, Object>();
		 
		roleService.delete(role_id);
		model.put("success", true);
		 
		return model ;
		
	}
	
	@ResponseBody
	@RequestMapping("/updateRole")  
	@Perm(id="ma_updateRole" , model="基础数据" , name="编辑角色名称" ,parentId="ma_roles",isPage=false , optType=OptType.UPDATE)
	public ResultModel updateRole(Role role) {
		
		ResultModel model = new ResultModel();
		 
		String res = roleService.update(role);
		if(res != null){
			model.setError(res);
		}else{
			model.put("data", role);
			model.setSuccess();
		}
		 
		return model ;
		
	}
	
	/**
	 * 返回角色对应的权限菜单 项目
	 * @return
	 */
	@RequestMapping("/findRoleMenus")
	@ResponseBody
	@Perm(id="ma_menusRole" , model="基础数据" , name="查询角色权限" ,parentId="ma_roles",isPage=false)
	public List<Menu> findRoleMenus(){
		 
		return menuService.findAll(true, true, true);
	}
	
	
	 
	 /**
	  * 更新角色权限
	  * @param role_id 角色ID
	  * @param perms 角色权限
	  * @return
	  */
	 @RequestMapping("/editRolePerms")
	 @ResponseBody
	 @Perm(id="ma_editRolePerms" , model="基础数据" , name="更新角色权限" ,parentId="ma_roles",isPage=false , optType=OptType.UPDATE)
	 public ResultModel updateRolePerms(Integer role_id , String perms) {
		 ResultModel  model = new ResultModel();
		 if(role_id == null){
			 model.setError("角色为空");
			 return model ;
		 }
		 
		 roleService.updateRolePerms(role_id, perms);
		 
		 model.setSuccess();
		 
		 return model ;
	 }
	
	//########################### 角色  结束 ############################################
	 
	//########################### 菜单 开始  ############################################
	/**
	 * 查找角色信息 
	 * @return
	 */
	@RequestMapping("/findMenus")
	@ResponseBody 
	@Perm(id="ma_findMenus" , model="基础数据" , name="获取菜单列表" ,parentId="ma_menus",isPage=false )
	public List<Menu> findMenus() {
		return menuService.findAll(true, true, false);
		 
	}
	
	@ResponseBody
	@RequestMapping("/addMenu")  
	@Perm(id="ma_addMenu" , model="基础数据" , name="添加菜单" ,parentId="ma_menus",isPage=false , optType=OptType.SAVE)
	public ResultModel addMenu(Menu menu  ) {
		
		ResultModel model = new ResultModel();
		 
		String res = menuService.add(menu);
		if(res != null){
			model.setError(res);
		}else{
			model.put("data", menu);
			model.setSuccess();
		}
		 
		return model ;
		 
		
	}
	@ResponseBody
	@RequestMapping("/removeMenu")  
	@Perm(id="ma_removeMenu" , model="基础数据" , name="删除菜单" ,parentId="ma_menus",isPage=false , optType=OptType.REMOVE)
	public Map<String, Object> removeMenu(String menu_id ) {
		
		Map<String, Object> model = new HashMap<String, Object>();
		 
		menuService.delete(menu_id);
		model.put("success", true);
		 
		return model ;
		
	}
	
	@ResponseBody
	@RequestMapping("/updateMenu")  
	@Perm(id="ma_updateMenu" , model="基础数据" , name="编辑菜单" ,parentId="ma_menus",isPage=false , optType=OptType.UPDATE)
	public ResultModel updateMenu(Menu menu ) {
		
		ResultModel model = new ResultModel();
		 
		String res = menuService.update(menu);
		if(res != null){
			model.setError(res);
		}else{
			model.put("data", menu);
			model.setSuccess();
		}
		 
		return model ;
		
	}
	
	
	//########################### 菜单 结束  ############################################
	
	@Autowired
	private HttpSession session;
	
	private User getSessionUser() {
		Object user  = session.getAttribute("user");
		
		if(user  != null && user instanceof User) {
			 //重新获取user 加入session
	        User u = (User)user;
	        return u ;
		}
		
		return null;
	}
	
}
